//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.UIDesigner.ActivityImportWizards
{
    using System.Collections.Generic;
    using System.Windows.Controls;
    using Microsoft.Research.DataLayer;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities;
    using System.Windows.Input;
    using System;

    /// <summary>
    /// Interface for all the Model for different activity wizards.
    /// </summary>
    public interface IActivityModel
    {
    }

    /// <summary>
    /// Interface for all the presenters for different activity wizards.
    /// </summary>
    public interface IActivityPresenter
    {
    }

    /// <summary>
    /// Interface for all the screens in wizard.
    /// </summary>
    public interface IActivityWizardScreens
    {
        // All screen should implement these venets.
        event EventHandler<ActivityImportEventArgs> NextButtonEventHandler;
        event EventHandler BackButtonEventHandler;
        event EventHandler CancelButtonEventHandler;
        event EventHandler OnWindowMoveEventHandler;
    }

    /// <summary>
    /// Activity Wizard class. This will keep reference of all the classes.
    /// </summary>
    public partial class ActivityWizard
    {
        IActivityPresenter wizardPresenter;

        // Stack of wizard screens
        internal Stack<IActivityWizardScreens> availableScreens =
            new Stack<IActivityWizardScreens>();

        // Current screen in the wizard.
        internal IActivityWizardScreens currentScreen;

        // First screen in the wizard.
        internal IActivityWizardScreens firstScreen;

        /// <summary>
        /// Initializes a new instance of the <see cref="ActivityWizard"/> class.
        /// </summary>
        /// <param name="activityToImport">The activity to import.</param>
        public ActivityWizard(ActivityWizardType activityToImport, Connection registryConnection)
        {
            this.InitializeComponent();

            // Read the txt skeleton files.
            ActivityCodeGenerator.ReadTemplateFiles();

            // It will start the wizard for different activities eg: WebService or StoredProcedure.
            switch (activityToImport)
            {
                case ActivityWizardType.WebService:
                    // Initialize the WS wizard presenter.
                    wizardPresenter = new WSWizardPresenter(registryConnection);

                    // assign the first screen.
                    firstScreen = new WSActivitySelection(wizardPresenter);
                    break;
            }

            // Display the first screen to container.
            UpdateView(firstScreen);

            // Add the first screen to container.
            availableScreens.Push(firstScreen);
        }

        /// <summary>
        /// Updates the view according to current screen.
        /// </summary>
        /// <param name="selectedScreen">The selected screen.</param>
        private void UpdateView(IActivityWizardScreens selectedScreen)
        {
            currentScreen = selectedScreen;

            currentScreen.NextButtonEventHandler += new EventHandler<ActivityImportEventArgs>(currentScreen_NextBtnEventHandler);
            currentScreen.BackButtonEventHandler += new EventHandler(currentWindow_bckBtnHandler);
            currentScreen.CancelButtonEventHandler += new EventHandler(currentWindow_cncBtnHandler);
            currentScreen.OnWindowMoveEventHandler += delegate(object sender, EventArgs e) { if (this.IsVisible && Mouse.LeftButton == MouseButtonState.Pressed) { this.DragMove(); } };
            
            // Add the current screen to container.
            this.LayoutRoot.Children.Clear();
            this.LayoutRoot.Children.Add(currentScreen as UserControl);
        }

        void currentScreen_NextBtnEventHandler(object sender, ActivityImportEventArgs e)
        {
            if (e == null || null == e.NextScreen)
            {
                // In case of import again, clear all previous data.
                currentScreen = firstScreen;
                availableScreens.Clear();
            }
            else
            {
                // Make next screen as current screen.
                currentScreen = e.NextScreen;
            }

            // Update the view according to current screen.
            UpdateView(currentScreen);

            // Add this current screen to container.
            availableScreens.Push(currentScreen);
        }

        void currentWindow_bckBtnHandler(object sender, EventArgs e)
        {
            // Remove current screen
            if (availableScreens.Count > 1)
                availableScreens.Pop();

            // Get the current screen.
            currentScreen = availableScreens.Peek();

            // Update the view according to current screen.
            UpdateView(currentScreen);
        }

        /// <summary>
        /// Currents the window_cnc BTN handler.
        /// </summary>
        void currentWindow_cncBtnHandler(object sender, EventArgs e)
        {
            Close();
        }
    }

    /// <summary>
    /// Enum for activity types.
    /// </summary>
    public enum ActivityWizardType
    {
        /// <summary>
        /// WebService activity import.
        /// </summary>
        WebService,
        /// <summary>
        /// Stored procedure activity import.
        /// </summary>
        StoredProcedure,
    }
}